/*
 * File:   main.cpp
 * Author: song
 *
 * Created on 2010年10月28日, 下午12:23
 */

#include <QtGui/QApplication>
#include "Data.h"
#include "Locate.h"
#include "Track.h"
#include "Control.h"
#include "PreProcess.h"
#include<omp.h>

extern int numFrame;
extern int hdims;
extern int minT;



//启动摄像头录制模块
void StartCam()
{
    Camera cam0;

    //图像预处理类
    PreProcess myPrePro0(&cam0);
    //目标检测类
    Locate myLoc0(&cam0);
    //目标跟踪类
    Track myTrack0(&cam0);
    //控制类
    Control myCon;
    
    if(myCon.OpenCam(cam0, -1) == true){
        double time;
        clock_t start, end;

        do{
            start = clock();

            //判断是否获取成功
            if(!cam0.pFrame){
                cout<<"图像获取失败"<<endl;
                return ;
            }
            myPrePro0.Process(cam0.pFrame);
            ///////////////调试代码//////////////////////////////////
            cvConvert(cam0.pBkMat, cam0.pBkImg);
            //cvConvert(pFrMat, pFrImg);
            //cvConvert(pGrayMat, pGray);

            if(numFrame > 50)
            {
                myLoc0.scan(cam0.pFrImg);
                myTrack0.Clear();
                int n = myLoc0.V.size();
#pragma omp parallel
                {
                int id = omp_get_thread_num();
                //cout<< "id:"<<id<<endl;
#pragma omp for
                for(int i = 0; i < n; ++i)
                {
                    CvScalar color;
                    color.val = {255, 0, 0, 0};
                    cvRectangle(cam0.pFrImg, myLoc0.V[i].pt0, myLoc0.V[i].pt1, color);
                    myTrack0.CamShift(myLoc0.V[i]);
                }
                }
                myTrack0.Draw(cam0.pFrame);
                cvShowImage("DetectorVPA1.0", cam0.pFrame);
            }


            cvShowImage("bk", cam0.pBkImg);
            cvShowImage("fr", cam0.pFrImg);

            ///////////////////////////////////////////////////////
            end = clock();
            time = (double)(end - start)/CLOCKS_PER_SEC * 1000;
            cout<<time<<endl;
            time = 50 - time;
            if(time <= 0) time = 1;
            cvWaitKey(time);

        }
        while(cam0.pFrame = cvQueryFrame(cam0.capture));
    }
}

//启动avi文件读取模块
void StartAvi(char* path)
{
    Camera cam0;

    //图像预处理类
    PreProcess myPrePro0(&cam0);
    //目标检测类
    Locate myLoc0(&cam0);
    //目标跟踪类
    Track myTrack0(&cam0);
    //控制类
    Control myCon;

    if(myCon.OpenAvi(cam0, path) == true){
        double time;
        clock_t start, end;

        do{
            start = clock();

            //判断是否获取成功
            if(!cam0.pFrame){
                cout<<"图像获取失败"<<endl;
                return ;
            }
            myPrePro0.Process(cam0.pFrame);
            ///////////////调试代码//////////////////////////////////
            cvConvert(cam0.pBkMat, cam0.pBkImg);
            //cvConvert(pFrMat, pFrImg);
            //cvConvert(pGrayMat, pGray);

            if(numFrame > 50)
            {
                myLoc0.scan(cam0.pFrImg);
                myTrack0.Clear();
                int n = myLoc0.V.size();
#pragma omp parallel
                {
                int id = omp_get_thread_num();
                //cout<< "id:"<<id<<endl;
#pragma omp for
                for(int i = 0; i < n; ++i)
                {
                    CvScalar color;
                    color.val = {255, 0, 0, 0};
                    cvRectangle(cam0.pFrImg, myLoc0.V[i].pt0, myLoc0.V[i].pt1, color);
                    myTrack0.CamShift(myLoc0.V[i]);
                }
                }
                myTrack0.Draw(cam0.pFrame);
                cvShowImage("DetectorVPA1.0", cam0.pFrame);
            }


            cvShowImage("bk", cam0.pBkImg);
            cvShowImage("fr", cam0.pFrImg);

            ///////////////////////////////////////////////////////
            end = clock();
            time = (double)(end - start)/CLOCKS_PER_SEC * 1000;
            cout<<time<<endl;
            time = 50 - time;
            if(time <= 0) time = 1;
            cvWaitKey(time);

        }
        while(cam0.pFrame = cvQueryFrame(cam0.capture));
    }
}


int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    QApplication app(argc, argv);

//    omp_set_num_threads(10);
//    int a[1000];
//#pragma omp parallel shared(a)
//    {
//        int id = omp_get_num_threads();
//        cout<<"id:"<<id<<endl;
//#pragma omp for schedule(static, 2)
//        for(int i = 0; i < 10; i++)
//            a[i] = 10;
//    }
    //获取当前进程所在绝对路径
//    char link[100], path[100];
//    sprintf(link, "/proc/%d/exe", getpid());
//    readlink(link, path, sizeof(path));
//    printf("%s\n.", path);
    StartAvi("/home/song/桌面/test/test01.avi");
    // create and show your widgets here

    return 0;
}
